<?php
class Tweet extends AppModel{
	var $hasMany = array('TweetTag'=>array( 'className'=>'TweetTag',
											'foreignKey'=>'tweet_id'),
	
						 'TweetMention'=>array( 'className'=>'TweetMention',
												'foreignKey'=>'tweet_id'),
	
						 'TweetUrl'=>array( 'className'=>'TweetUrl',
											'foreignKey'=>'tweet_id'),
	
						 'TweetFavorite'=>array('className'=>'TweetFavorite',
												'foreignKey'=>'tweet_id'),
	
						 'TweetBox'=>array( 'className'=>'TweetBox',
											'foreignKey'=>'tweet_id'));//,
	
//						 'Reply'=>array( 'className'=>'Reply',
//										 'foreignKey'=>'tweet_id'));
	
	var $belongsTo = array('User'=>array('className'=>'User',
										 'foreignKey'=>'user_id'));
	
	function getReply($twId){
		if($listRep = $this->find('all',array('conditions'=>array('parent_id'=>$twId))))
			return $listRep;
		else return false;
	}
	
	function get_tweet_detail($t_id,$u_id){
		$this->unbindModel(array('hasMany'=>array('TweetBox','TweetTag','TweetMention','TweetUrl','TweetFavorite')),false);
		$info = $this->findById($t_id);
		
		if($info){
			App::uses('TweetFavorite','Model');
			App::uses('TweetBox','Model');
			$twFavorite = new TweetFavorite();
			$twBox = new TweetBox();
			
			$twFavorite->unbindModel(array('belongsTo'=>array('User','Tweet')),false);
			$img = IMG_UPLOAD_URL.'/'.end(explode("/",$info['Tweet']['media_url']));
			if($info['Tweet']['media_url'] && strstr($info['Tweet']['media_url'],"/"))
				$info['Tweet']['img_size'] = getimagesize($img);
			else $info['Tweet']['img_size'] = "Image not found";
			
			$info['Tweet']['total_favorite'] = $twFavorite->find('count',array('conditions'=>array( 'tweet_id'=>$t_id,
																									'untweet_flag !='=>1)));
			if($twFavorite->findByUserIdAndTweetIdAndUntweetFlag($u_id,$t_id,1))
				$info['Tweet']['is_fav'] = 'yes';
			else $info['Tweet']['is_fav'] = 'no';
			
			if($twBox->findByUserIdAndTweetIdAndRetweet($u_id,$t_id,1))
				$info['Tweet']['is_retweet'] = 'yes';
			else $info['Tweet']['is_retweet'] = 'no';
			
			$info['Tweet']['total_retweet'] = $twBox->find('count',array('conditions'=>array('tweet_id'=>$t_id,
																							'retweet'=>1)));
			$info['Tweet']['reply'] = $this->find('all',array('conditions'=>array('parent_id'=>$t_id)));
			if($info['Tweet']['reply'])
				$info['Tweet']['total_reply'] = count($info['Tweet']['reply']);
			else $info['Tweet']['total_reply'] = 0;
			
			$info['Tweet']['total_parent'] = $this->get_parent($t_id,false);
			
			
			return json_encode($info);
		}else return json_encode(array('Failure'));
	}
	
	function get_parent($t_id,$is_timeline = false){
		$this->unbindModel(array('belongsTo'=>array('User'),
									 'hasMany'=>array('TweetBox','TweetTag','TweetMention','TweetUrl','TweetFavorite')),false);
		$parent = $this->findById($t_id);
		if($is_timeline)
			$this->unbindModel(array('belongsTo'=>array('User'),
									 'hasMany'=>array('TweetBox','TweetTag','TweetMention','TweetUrl','TweetFavorite')),false);
		else $this->bindModel(array('belongsTo'=>array('User'),
									 'hasMany'=>array('TweetBox','TweetTag','TweetMention','TweetUrl','TweetFavorite')),false);
		$flag = false;
		if($parent['Tweet']['parent_id'] !=0){
			$flag = true;
			$arrParent['total_parent'] = 0;
			while($parent['Tweet']['parent_id'] != 0){
				$parent = $this->findById($parent['Tweet']['parent_id']);
				if($parent){
					$arrParent['parent_detail'][] = $parent;
					$arrParent['total_parent']= $arrParent['total_parent'] + 1;
				}else{
					break;
				}
			}
			if($is_timeline)
				return array('total_parent'=>$arrParent['total_parent']);
			else return $arrParent; 
		}else return array('total_parent'=>0);
	}
	
}